package com.bdq.user.api.controller;

import cn.hutool.core.convert.Convert;
import com.bdq.common.entity.common.CommonResult;
import com.bdq.common.entity.common.PageResult;
import com.bdq.common.feign.content.vo.ContentListVO;
import com.bdq.user.api.model.param.*;
import com.bdq.user.api.model.vo.*;
import com.bdq.user.application.model.cmd.LoginCmd;
import com.bdq.user.application.model.cmd.SendLoginVerifyCodeCmd;
import com.bdq.user.application.model.cmd.UserUpdateCmd;
import com.bdq.user.application.model.cmd.ViewRegionLevelUpdateCmd;
import com.bdq.user.application.model.dto.*;
import com.bdq.user.application.model.query.ListFollowUserQry;
import com.bdq.user.application.model.query.UserContentPageQry;
import com.bdq.user.application.service.UserAppCmdService;
import com.bdq.user.application.service.UserAppQueryService;
import com.bdq.user.infrastructure.util.TencentImUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * bdq-user登录相关
 *
 * @author malei
 * @date 2022-11-24 21:41
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserAppCmdService userAppCmdService;

    @Resource
    private UserAppQueryService userAppQueryService;


    /**
     * 发送登录验证码
     *
     * @param param
     * @return
     */
    @PostMapping("/login-verify-code/send")
    public CommonResult<Boolean> sendLoginVerifyCode(@RequestBody @Validated SendLoginVerifyCodeParam param) {
        userAppCmdService.sendLoginVerifyCode(Convert.convert(SendLoginVerifyCodeCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 登录
     *
     * @param param
     * @return
     */
    @PostMapping("/login")
    public CommonResult<LoginVO> login(@RequestBody @Validated LoginParam param) {
        LoginAppDTO loginAppDTO = userAppCmdService.login(Convert.convert(LoginCmd.class, param));
        return CommonResult.success(Convert.convert(LoginVO.class, loginAppDTO));
    }

    /**
     * 登出
     *
     * @return
     */
    @PostMapping("/logout")
    public CommonResult<Boolean> logout() {
        userAppCmdService.logout();
        return CommonResult.success(true);
    }

    /**
     * 用户详情
     *
     * @return
     */
    @GetMapping("/detail")
    public CommonResult<UserVO> userDetail() {
        UserAppDTO userAppDTO = userAppQueryService.userDetail();
        return CommonResult.success(Convert.convert(UserVO.class, userAppDTO));
    }

    /**
     * 用户查询
     *
     * @return
     */
    @GetMapping("/query")
    public CommonResult<UserQueryVO> userQuery(@RequestParam(name = "userId") Long userId) {
        UserQueryAppDTO userQueryAppDTO = userAppQueryService.userQuery(userId);
        return CommonResult.success(Convert.convert(UserQueryVO.class, userQueryAppDTO));
    }

    /**
     * 用户更新
     *
     * @return
     */
    @PostMapping("/update")
    public CommonResult<Boolean> userUpdate(@RequestBody UserUpdateParam param) {
        userAppCmdService.userUpdate(Convert.convert(UserUpdateCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 获取浏览区域级别
     *
     * @return
     */
    @GetMapping("/view-region-level/get")
    public CommonResult<List<ViewRegionLevelVO>> getViewRegionLevel() {
        List<ViewRegionLevelDTO> dtoList = userAppQueryService.getViewRegionLevel();
        return CommonResult.success(Convert.toList(ViewRegionLevelVO.class, dtoList));
    }

    /**
     * 浏览区域级别更新
     *
     * @return
     */
    @PostMapping("/view-region-level/update")
    public CommonResult<Boolean> updateViewRegionLevel(@RequestBody ViewRegionLevelUpdateParam param) {
        userAppCmdService.updateViewRegionLevel(Convert.convert(ViewRegionLevelUpdateCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 用户内容分页查询
     *
     * @param param
     * @return
     */
    @GetMapping("/content/page")
    public CommonResult<PageResult<ContentListVO>> contentPage(@Validated UserContentPageParam param){
        return CommonResult.success(userAppQueryService.contentPage(Convert.convert(UserContentPageQry.class, param)));
    }

    /**
     * 关注
     *
     * @param userId
     * @return
     */
    @GetMapping("/follow")
    public CommonResult<Boolean> follow(Long userId){
        userAppQueryService.follow(userId);
        return CommonResult.success(true);
    }

    /**
     * 取消关注
     *
     * @param userId
     * @return
     */
    @GetMapping("/follow/cancel")
    public CommonResult<Boolean> cancelFollow(Long userId){
        userAppQueryService.cancelFollow(userId);
        return CommonResult.success(true);
    }

    /**
     * 分页查询关注列表
     *
     * @param param
     * @return
     */
    @GetMapping("/follow/page")
    public CommonResult<PageResult<UserSimpleVO>> listFollow(ListFollowUserParam param){
        PageResult<UserSimpleAppDTO> pageResult = userAppQueryService.listFollow(Convert.convert(ListFollowUserQry.class, param));
        return CommonResult.success(new PageResult<>(Convert.toList(UserSimpleVO.class, pageResult.getList()), pageResult.getTotal()));
    }

    @GetMapping("/test")
    public void test() {
        TencentImUtil.singleAccountImport(1L, "盗版书", "");
    }
}
